#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>


#include "packets/packet.h"
#include "packets/CRC.h"

#include "communication/input.h"
#include "communication/comPort.h"
#include "features/adc/adc.h"
#include "board/board.h"

#include "packets/structs/msgHeaderDefs.h"
#include "packets/structs/encPacketStructs.h"
#include "packets/structs/adcPacketStructs.h"

#include "features/dio/dio.h"
#include "features/dac/dac.h"


#include "test/test.h"

volatile uint8_t USB_data[32];



uint16_t i,timeoutInc;
int main (void)
{	
	brdInit();					//setup of any board specific items

/*	
	PORTD.DIRSET = 0x0F;
	PORTE.DIRSET = 0x06;
	TCD0.CTRLA = TC_CLKSEL_DIV1_gc;
	TCD0.CCA = 800;
*/	 		
#ifndef _DEBUG
	uint8_t eepromAddr;
	int16_t *CAL_ptr = &daqCal.ADC_OFFSET_CAL_2X[0];
	
	for (eepromAddr=0;eepromAddr<=48;eepromAddr+=2)
	{
		*CAL_ptr++ = eeprom_read_word(eepromAddr);
	}
#endif
	sei();

	sendDaqReady(); 

	while (1) 
	{	
		
		if (dmaDataInBuf())
		{
			if(comPortGet((uint8_t *)USB_data))globalMsgHandler((uint8_t *)USB_data);
			asm("nop");
		}	
	
		
	}
}

//catch all of bad interrupts
ISR(BADISR_vect)
{
	asm("nop");
}


